package com.xltq.conformity.controller;

import com.github.pagehelper.PageInfo;
import com.mysql.cj.xdevapi.Table;
import com.xltq.conformity.dao.AdminUserDao;
import com.xltq.conformity.entity.AdminUser;
import com.xltq.conformity.service.AdminUserService;
import com.xltq.conformity.vo.AdminUserVO;
import com.xltq.conformity.vo.MessageVo;
import com.xltq.conformity.vo.PageVo;
import com.xltq.conformity.vo.TableVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 王权富贵
 */
@RestController
@RequestMapping(value = "/user")
public class AdminUserController {
    @Autowired
    private AdminUserService adminUserService;

    @ApiOperation("查询所有数据,前端可传递分页页码和页面数据数量,并传递相应的查询数据参数")
    @RequestMapping(value = "/getAllUser", method = RequestMethod.GET)
    public TableVo<AdminUser> getAllUser(PageVo<AdminUser> pageVo, AdminUser adminUser) {
        PageInfo<AdminUser> pageInfo = adminUserService.queryAll(pageVo, adminUser);
        TableVo<AdminUser> tableVo = new TableVo<>();
        tableVo.setCode(0);
        tableVo.setCount(pageInfo.getTotal());
        tableVo.setData(pageInfo.getList());
        tableVo.setMsg("成功");
        return tableVo;
    }

    @ApiOperation("删除数据,支持批量删除")
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public MessageVo del(Integer[] ids) {
        MessageVo messageVo = new MessageVo();
        Boolean del = adminUserService.del(ids);
        if (del) {
            messageVo.setStatus(true);
            messageVo.setMessage("删除成功");
            messageVo.setCode("1");
        } else {
            messageVo.setStatus(true);
            messageVo.setMessage("删除成功");
            messageVo.setCode("0");
        }
        return messageVo;
    }

    @ApiOperation("添加数据")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public MessageVo add(AdminUser adminUser) {
        MessageVo messageVo = new MessageVo();
        Boolean add = adminUserService.add(adminUser);
        if (add) {
            messageVo.setCode("1");
            messageVo.setStatus(true);
            messageVo.setMessage("添加成功");
        } else {
            messageVo.setCode("0");
            messageVo.setStatus(false);
            messageVo.setMessage("添加失败");
        }
        return messageVo;
    }

    @ApiOperation("修改数据")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public MessageVo update(AdminUserVO adminUser) {
        MessageVo messageVo = new MessageVo();
        AdminUser adminUser1 = new AdminUser(adminUser.getId(), adminUser.getPassword(), adminUser.getEmail(), adminUser.getUsername(), adminUser.getStatus());
        Boolean update = adminUserService.update(adminUser1);
        if (update) {
            messageVo.setCode("1");
            messageVo.setStatus(true);
            messageVo.setMessage("修改成功");
        } else {
            messageVo.setCode("0");
            messageVo.setStatus(false);
            messageVo.setMessage("修改失败");
        }
        return messageVo;
    }
}
